package com.ganjiangps.huochetou.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ganjiangps.BaseCRUDController;
import com.ganjiangps.huochetou.common.util.StringUtil;
import com.ganjiangps.huochetou.model.PersonalManagement;
import com.ganjiangps.huochetou.service.PersonalManagementNoQuoteService;
import com.ganjiangps.huochetou.service.PersonalManagementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 名单设置controller
 * 
 * @author xiaoy
 */
@Controller
@RequestMapping("/admin/personalManagement")
public class PersonalManagementController extends BaseCRUDController<PersonalManagement> {

	@Resource
	private PersonalManagementService personalManagementService;

	@Resource
	private PersonalManagementNoQuoteService personalManagementNoQuoteService;

	@RequestMapping("menu")
	public String menu() {
		return "/admin/index";
	}

	@Override
	protected void preList(Model model, PersonalManagement entity, String orderBy) throws IllegalAccessException {
		PersonalManagement afterhandleEntity = changeNullStringToNull(entity);

		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");

		PageHelper.orderBy("id desc");

		Map<String, Object> map = new HashMap<>();

		initPage(map, pageNum, pageSize);

		List<PersonalManagement> list = personalManagementService
				.selectList(new EntityWrapper<PersonalManagement>(afterhandleEntity));

		PageInfo pagehelper = initPagehelper(map, list);

		model.addAttribute("pagehelper", pagehelper);

		model.addAttribute("sf", sf);

		model.addAttribute("personalManagement", afterhandleEntity);

	}

	@Override
	protected boolean preInsert(PersonalManagement entity, Map<String, Object> map) {
		// TODO Auto-generated method stub
		Integer pmType = entity.getPmtype();
		if (pmType.equals(1)) {
			entity.setPmno("DM" + StringUtil.getNo16());

		}
		if (pmType.equals(2)) {
			entity.setPmno("XM" + StringUtil.getNo16());

		}

		entity.setAddtime(new Date());
		return true;
	}

	@PostMapping("validatePmname")
	@ResponseBody
	protected Map<String, Object> validatePmname(String pmname, Long id) {
		Map<String, Object> map = new HashMap<>();
		map.put("valid", false);

		Wrapper<PersonalManagement> wrapper = Condition.create().eq("pmname", pmname);

		PersonalManagement personalManagement = personalManagementService.selectOne(wrapper);
		if (personalManagement == null) {
			map.put("valid", true);

		}

		return map;
	}

	@Override
	protected void preDeleteView(Model model, Long id) {
		PersonalManagement personalManagement = personalManagementService.selectById(id);
		model.addAttribute("personalManagement", personalManagement);

	}

	@PostMapping("getPersonalManagement")
	@ResponseBody
	public PersonalManagement getPersonalManagement(String pmno) {
		PersonalManagement personalManagement = personalManagementService
				.selectOne(new EntityWrapper<PersonalManagement>().eq("pmno", pmno));
		return personalManagement;
	}
}